package wetogether.community.service;

import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import wetogether.community.dao.BoardDAO;
import wetogether.community.vo.BoardVO;
import wetogether.community.vo.CommentsVO;
import wetogether.community.vo.ListVO;
import wetogether.community.vo.PagingBean;

 class QnaBoardServiceImpl implements BoardService {
	private BoardDAO boardDAO;
	private Log log=LogFactory.getLog(getClass());
	
	public void setBoardDAO(BoardDAO boardDAO) {
		this.boardDAO=boardDAO;
	}
	
	@Override
	public void addComments(CommentsVO commentsVO) throws SQLException {
		// TODO Auto-generated method stub

	}
	
	/**
	 * seq가져와 vo에 set하고 insert
	 * seq를 이용하여 입력된 글의 시간을 가져와 vo에 set하고 화면에 뿌린다.
	 */
	@Override
	public void addContent(BoardVO boardVO) throws SQLException {
		log.debug("[SERVICE]in addContent");
		int seq=boardDAO.getSelectContentSeq();
		if(boardVO.getGroupDepth()==0)
			boardVO.setGroupId(seq);			
		boardVO.setContentId(seq);		
		boardDAO.insertContent(boardVO);
		String date=boardDAO.getDate(seq);
		boardVO.setRegDate(date);
		boardVO.setModDate(date);
		if(boardVO.getAttachmentVO()!=null) {
			int attSeq=boardDAO.getSelectAttachmentSeq();
			boardVO.getAttachmentVO().setFileId(attSeq);
			boardVO.getAttachmentVO().setContentId(seq);
			boardDAO.insertAttachment(boardVO.getAttachmentVO());
		}		
	}

	@Override
	public void deleteComments(int contentId) throws SQLException {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteContent(int contentId) throws SQLException {
		boardDAO.deleteContent(contentId);

	}

	@Override
	public BoardVO getContent(int contentId) throws SQLException {
		log.debug("[SERVICE]in getContent");
		log.debug("contentId: "+contentId);
		BoardVO vo=boardDAO.getContent(contentId);
		vo.setAttachmentVO(boardDAO.getAttachment(contentId));
		boardDAO.updateHits(contentId);
		System.out.println("return boardVO : "+vo);
		return vo;
	}

	@Override
	public ListVO getContentList(int locationId, int boardId, int page)
			throws SQLException {
		log.debug("[SERVICE]in getContentList");
		System.out.println("locId="+locationId+" boardId="+boardId+" page="+page);
		return new ListVO(boardDAO.getContentList(locationId, boardId, page),
				new PagingBean(boardDAO.getTotalContents(boardId, locationId), page), boardId, locationId);
	}

	@Override
	public void modifyContent(int contentId) throws SQLException {
		// TODO Auto-generated method stub

	}
	
	/**
	 * date : 2010-11-07
	 * author : Shin In-soo
	 * comment :	
	 * 		답글을 삽입하는 메너드
	 * 		insertPos : 답글을 넣을 position을 가지는 변수
	 * 		checkGroupPosition() : 답글을 삽입할 위치를 리턴하는 메서드 
	 * 			-리턴값이 null(-1) : 제일 뒤(Max(position)+1)에다 삽입
	 * 			-리턴값이 숫자 : 그 위치에 삽입  
	 */
	@Override
	public void addReply(BoardVO boardVO) throws SQLException {
		log.debug("[SERVICE]in addReply");
		int insertPos=boardDAO.checkGroupPosition(boardVO);
		System.out.println("insertPos="+insertPos);
		if(insertPos==-1) {
			insertPos=boardDAO.findMaxGroupPosition(boardVO)+1;
		}
		boardVO.setGroupPosition(insertPos);
		boardVO.setGroupDepth(boardVO.getGroupDepth()+1);
		this.addContent(boardVO);
	}	
	
}
